package com.ruoyi.system.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WebRecycleType;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserCardRecharge;
import com.ruoyi.system.domain.WebUserCardTime;
import com.ruoyi.system.service.IWebRecycleTypeService;
import com.ruoyi.system.service.IWebUserCardRechargeService;
import com.ruoyi.system.service.IWebUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 充值服务Controller
 * 
 * @author wsliupan@126.com
 * @date 2021-06-27
 */
@Controller
@RequestMapping("/system/UserCardRecharge")
public class WebUserCardRechargeController extends BaseController
{
    private String prefix = "system/UserCardRecharge";

    @Autowired
    private IWebUserCardRechargeService webUserCardRechargeService;
    @Autowired
    private IWebUserService iWebUserService;

    @RequiresPermissions("system:UserCardRecharge:view")
    @GetMapping()
    public String UserCardRecharge()
    {
        return prefix + "/UserCardRecharge";
    }

    /**
     * 查询充值服务列表
     */
    @RequiresPermissions("system:UserCardRecharge:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WebUserCardRecharge webUserCardRecharge)
    {
        startPage();
        List<WebUserCardRecharge> list = webUserCardRechargeService.selectWebUserCardRechargeList(webUserCardRecharge);
        return getDataTable(list);
    }

    /**
     * 导出充值服务列表
     */
    @RequiresPermissions("system:UserCardRecharge:export")
    @Log(title = "充值服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WebUserCardRecharge webUserCardRecharge)
    {
        List<WebUserCardRecharge> list = webUserCardRechargeService.selectWebUserCardRechargeList(webUserCardRecharge);
        ExcelUtil<WebUserCardRecharge> util = new ExcelUtil<WebUserCardRecharge>(WebUserCardRecharge.class);
        return util.exportExcel(list, "充值服务数据");
    }

    /**
     * 新增充值服务
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        WebUser user = new WebUser();
        List<WebUser> users = iWebUserService.selectWebUserList(user);
        mmap.put("users", JSON.toJSONString(users));
//        WebRecycleType webRecycleType = new WebRecycleType();
//        webRecycleType.setType(1);
//        webRecycleType.setParentId(0L);
//        List<WebRecycleType> listRecycleType = iwebRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
//        mmap.put("listRecycleType",listRecycleType);
        return prefix + "/add";
    }

    /**
     * 新增保存充值服务
     */
    @RequiresPermissions("system:UserCardRecharge:add")
    @Log(title = "充值服务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WebUserCardRecharge webUserCardRecharge)
    {
        webUserCardRecharge.setMoney(webUserCardRecharge.getCountMoney());
        return toAjax(webUserCardRechargeService.insertWebUserCardRecharge(webUserCardRecharge));
    }

    /**
     * 修改充值服务
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WebUserCardRecharge webUserCardRecharge = null;
        if(id == null || id ==  -1){
            webUserCardRecharge = new WebUserCardRecharge();
        }else{
            webUserCardRecharge = webUserCardRechargeService.selectWebUserCardRechargeById(id);
        }
        mmap.put("webUserCardRecharge", webUserCardRecharge);
        return prefix + "/edit";
    }

    /**
     * 修改保存充值服务
     */
    @RequiresPermissions("system:UserCardRecharge:edit")
    @Log(title = "充值服务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WebUserCardRecharge webUserCardRecharge)
    {
        return toAjax(webUserCardRechargeService.updateWebUserCardRecharge(webUserCardRecharge));
    }

    /**
     * 删除充值服务
     */
    @RequiresPermissions("system:UserCardRecharge:remove")
    @Log(title = "充值服务", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(webUserCardRechargeService.deleteWebUserCardRechargeByIds(ids));
    }

    /**
     * 消费
     * @param webUserCardRecharge
     * @param mmap
     * @return
     */
    @GetMapping("/consumption")
    public String consumption(WebUserCardRecharge webUserCardRecharge,ModelMap mmap){

        webUserCardRecharge = webUserCardRechargeService.selectWebUserCardRechargeById(webUserCardRecharge.getId());
        mmap.put("webUserCardRecharge", webUserCardRecharge);
        return prefix + "/consumption";
    }
    /**
     * 消费
     * @param webUserCardRecharge
     * @return
     */
    @PostMapping("/consumption")
    @ResponseBody
    public AjaxResult consumptionSave(WebUserCardRecharge webUserCardRecharge){
        return toAjax(webUserCardRechargeService.consumption(webUserCardRecharge));
    }

    /**
     * 充值
     * @param webUserCardRecharge
     * @return
     */
    @PostMapping("/chongzhi")
    @ResponseBody
    public AjaxResult chongzhi(WebUserCardRecharge webUserCardRecharge){
        return toAjax(webUserCardRechargeService.chongzhi(webUserCardRecharge));
    }

    /**
     * 转换次卡
     * @param webUserCardRecharge
     * @param mmap
     * @return
     */
    @GetMapping("/transition")
    public String transition(WebUserCardRecharge webUserCardRecharge,ModelMap mmap){

        webUserCardRecharge = webUserCardRechargeService.selectWebUserCardRechargeById(webUserCardRecharge.getId());
        WebUser user = iWebUserService.getById(webUserCardRecharge.getUserId());
        mmap.put("user", user);

        WebRecycleType webRecycleType = new WebRecycleType();
        webRecycleType.setType(1);
        webRecycleType.setParentId(0L);
        List<WebRecycleType> listRecycleType = iwebRecycleTypeService.selectWebRecycleTypeList(webRecycleType);
        mmap.put("listRecycleType",listRecycleType);
        mmap.put("webUserCardRecharge",webUserCardRecharge);

        return prefix + "/transition";
    }

    /**
     * 转换次卡
     * @param webUserCardRecharge
     * @param userCardTime
     * @return
     */
    @PostMapping("/transition")
    @ResponseBody
    public AjaxResult transitionSave(WebUserCardRecharge webUserCardRecharge , WebUserCardTime userCardTime){
        return toAjax(webUserCardRechargeService.transitionSave(webUserCardRecharge,userCardTime));
    }

    @Autowired
    private IWebRecycleTypeService iwebRecycleTypeService;

}
